package com.xkdx.guangguang.fragment;

import java.util.HashMap;

import wealk.android.jewels.Jewels;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.LoginActivity;
import com.xkdx.guangguang.fragment.user.UserLoginFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.module.util.ErrorModule;

public abstract class BaseFragment extends Fragment {
	public ProgressDialog dataLoadDialog;
	public RefreshFragment refreshFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		refreshFragment = new RefreshFragment();
		dataLoadDialog = new ProgressDialog(getActivity());
		
		dataLoadDialog.setMessage("读取中,请耐心等待...");
		dataLoadDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dataLoadDialog.setCancelable(true);
		dataLoadDialog.setCanceledOnTouchOutside(false);// 解决4.0以上系统触屏后执行onCanel方法
	}

	public void showErrorTip(HashMap<String, AbsModule> map) {
		if (getActivity() != null) {
			Toast.makeText(getActivity(), ((ErrorModule) map.get("1")).errorMessage, Toast.LENGTH_SHORT).show();

		}
	}

	public void refresh(HashMap<String, AbsModule> map) {
		if (getActivity() != null && ((ErrorModule) map.get("1")).errorMessage!=null) {
			Toast.makeText(getActivity(), ((ErrorModule) map.get("1")).errorMessage, Toast.LENGTH_SHORT).show();
			getFragmentManager().beginTransaction().replace(R.id.fragment, refreshFragment).commitAllowingStateLoss();
		}
	}

	public void refreshShop() {
		if (getActivity() != null) {
			Toast.makeText(getActivity(), getString(R.string.not_location_current_position), Toast.LENGTH_SHORT).show();
			getFragmentManager().beginTransaction().replace(R.id.fragment, refreshFragment).addToBackStack(null)
					.commitAllowingStateLoss();
		}
	}

	public void reSubmit() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("提交失败");
		builder.setNegativeButton("重新提交", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				showLoading();
			}
		});
		builder.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				Jewels.SUBMITSCORE = 0;
			}
		});
		builder.create().show();
	}

	public void SelfUserLogin() {

		AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
		ab.setTitle("提示");
		ab.setMessage("请先登录");
		ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				UserLoginFragment userLoginFragment = new UserLoginFragment();
				getFragmentManager().beginTransaction().replace(R.id.fragment, userLoginFragment).addToBackStack(null)
						.commit();
			}
		});
		ab.setNegativeButton("取消", null);
		ab.create().show();

	}

	public void UserLogin() {

		AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
		ab.setTitle("提示");
		ab.setMessage("请先登录");
		ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				IConstants.BottomBtnSelected = 5;
				Intent intent = new Intent(getActivity(), LoginActivity.class);
				intent.putExtra("fragment", "UserLoginFragment");
				startActivity(intent);
			}
		});
		ab.setNegativeButton("取消", null);
		ab.create().show();

	}

	public abstract void findView();

	public abstract void setOnClick();

	public abstract void showLoading();

	public abstract void showLoaded(HashMap<String, AbsModule> map);

	public abstract void showClick(HashMap<String, AbsModule> map);

	public abstract void showLoadMore(HashMap<String, AbsModule> map);

	@Override
	public void onDestroy() {
		if (dataLoadDialog != null && dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		super.onDestroy();
	}
}
